import java.util.ArrayList;
import java.util.List;

// 1. 中介者接口（定义通信方法）
interface ChatMediator {
    void sendMessage(String message, User user);
    void addUser(User user);
}

// 2. 具体中介者：聊天室
class ChatRoom implements ChatMediator {
    private List<User> users = new ArrayList<>();

    @Override
    public void addUser(User user) {
        users.add(user);
    }

    @Override
    public void sendMessage(String message, User sender) {
        // 广播消息给其他用户（排除发送者自身）
        for (User user : users) {
            if (user != sender) {
                user.receive(message);
            }
        }
    }
}

// 3. 抽象同事类（用户）
abstract class User {
    protected ChatMediator mediator;
    protected String name;

    public User(ChatMediator mediator, String name) {
        this.mediator = mediator;
        this.name = name;
    }

    public abstract void send(String message);
    public abstract void receive(String message);
}

// 4. 具体同事类：普通用户
class ChatUser extends User {
    public ChatUser(ChatMediator mediator, String name) {
        super(mediator, name);
    }

    @Override
    public void send(String message) {
        System.out.println(name + " 发送消息: " + message);
        mediator.sendMessage(message, this); // 通过中介者转发
    }

    @Override
    public void receive(String message) {
        System.out.println(name + " 收到消息: " + message);
    }
}

public class Main {
    public static void main(String[] args) {
        // 创建中介者（聊天室）
        ChatMediator chatRoom = new ChatRoom();

        // 创建用户并加入聊天室
        User alice = new ChatUser(chatRoom, "Alice");
        User bob = new ChatUser(chatRoom, "Bob");
        User charlie = new ChatUser(chatRoom, "Charlie");
        chatRoom.addUser(alice);
        chatRoom.addUser(bob);
        chatRoom.addUser(charlie);

        // Alice发送消息，Bob和Charlie接收
        alice.send("大家好！");
    }
}
